﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrbaCommerce.Historial_Cliente
{

    public partial class ListadoCalif: Form
    {
        private string strFiltros;
        private int user_id;

        // Page
        private int mintTotalRecords = 0;
        private int mintPageSize = 10;
        private int mintPageCount = 0;
        private int mintCurrentPage = 1;


        private void fillGrid()
        {
            // For Page view.
            //this.mintPageSize = int.Parse(this.bindingNavigator1.Text);
            this.mintTotalRecords = getCount();
            this.mintPageCount = this.mintTotalRecords / this.mintPageSize;

            // Adjust page count if the last page contains partial page.
            if (this.mintTotalRecords % this.mintPageSize > 0)
                this.mintPageCount++;

            this.mintCurrentPage = 0;

            loadPage();
        }

        private int getCount()
        {
            // This select statement is very fast compare to SELECT COUNT(*)

            SqlConnection con = Program.obtenerConexion();
            string strSql = "SELECT cal_codigo as 'Código'"
                + " FROM Monkey_Coders.Calificaciones"
                + strFiltros;
                
                /*"SELECT Rows FROM SYSINDEXES " +
                            "WHERE Id = OBJECT_ID('Monkey_Coders.Publicaciones') AND IndId < 2";*/
            

            SqlCommand cmd = new SqlCommand(strSql, con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "Monkey_Coders.Calificaciones");

            int intCount = ds.Tables[0].Rows.Count;


            cmd.Dispose();

            return intCount;
        }

        private void loadPage()
        {
            string strSql = "";
            int intSkip = 0;

            if (mintPageCount == 0)
                buttonAdelante.Enabled = false;

            SqlConnection con = Program.obtenerConexion();
            intSkip = (this.mintCurrentPage * this.mintPageSize);

            // Select only the n records.

            strSql = "SELECT TOP " + this.mintPageSize
                + " cal_codigo as 'Código', cal_cant_estrellas as 'Estrellas', cal_descripcion as 'Descripcion'"
                + " FROM Monkey_Coders.Calificaciones"
                + strFiltros
                + " AND cal_codigo NOT IN " +
                "(SELECT TOP " + intSkip +
                " cal_codigo "
                + " FROM Monkey_Coders.Calificaciones"
                + strFiltros + ")";

            SqlCommand cmd = new SqlCommand(strSql, con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            da.Fill(ds, "Monkey_Coders.Calificaciones");

            // Populate Data Grid
            dataGridView1.DataSource = ds.Tables["Monkey_Coders.Calificaciones"].DefaultView;

            // Show Status
            labelPaginas.Text = (this.mintCurrentPage + 1).ToString() +
              " / " + this.mintPageCount.ToString();

            cmd.Dispose();
            da.Dispose();
            ds.Dispose();
        }





        public ListadoCalif(int user_id)
        {
            InitializeComponent();
            this.user_id = user_id;
            comboBoxEstrellas.SelectedIndex = 0;

        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();

            strFiltros = " WHERE (cal_vendedor_id = " + user_id + " OR cal_usuario_id = " + user_id
                            + ") AND cal_descripcion like '%" + textBoxDesc.Text +"%'"
                            + " AND cal_cant_estrellas like '%" + Convert.ToInt32(comboBoxEstrellas.SelectedValue) +"%'";
            
            fillGrid();

        }

        private void buttonLimpiar_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();
        }

        private void buttonPrimera_Click(object sender, EventArgs e)
        {

            this.mintCurrentPage = 0;

            loadPage();
        }

        private void buttonAtras_Click(object sender, EventArgs e)
        {
            if (this.mintCurrentPage == this.mintPageCount)
                this.mintCurrentPage = this.mintPageCount - 1;

            this.mintCurrentPage--;

            if (this.mintCurrentPage < 1)
                this.mintCurrentPage = 0;

            loadPage();
        }

        private void buttonAdelante_Click(object sender, EventArgs e)
        {
            this.mintCurrentPage++;

            if (this.mintCurrentPage > (this.mintPageCount - 1))
                this.mintCurrentPage = this.mintPageCount - 1;

            loadPage();
        }

        private void buttonLast_Click(object sender, EventArgs e)
        {
            this.mintCurrentPage = this.mintPageCount - 1;
            loadPage();
        }

    }
}
